/*
    Copyright 2010 Barnet Wagman

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
 */

package com.norbl.util;

import java.io.*;

/**
 *
 * @author Barnet Wagman
 */
public class StandardSerializer implements Serializer {
        
    public class ObjectInputStreamWithPrimitiveSupport 
        extends ObjectInputStream {

        public ObjectInputStreamWithPrimitiveSupport(InputStream is)
            throws IOException, StreamCorruptedException {
            super(is);
        }

        public Class resolveClass(ObjectStreamClass desc)
            throws ClassNotFoundException, IOException {
            try {
                return super.resolveClass(desc);
            } catch (ClassNotFoundException e) {
                String nm = desc.getName();
                if (nm.equals("int")) return int.class;
                if (nm.equals("long")) return long.class;
                if (nm.equals("float")) return float.class;
                if (nm.equals("double")) return double.class;
                if (nm.equals("char")) return char.class;
                if (nm.equals("boolean")) return boolean.class;
                if (nm.equals("short")) return short.class;
                if (nm.equals("byte")) return byte.class;
                else /* check for other primitive types if one wishes.*/
                    throw e;
            }
        }
    }

        /** {@inheritDoc}
         *
         * @param obj
         * @return object as byte[].
         * @throws ot.OtException
         */
    public byte[] objectToByteArray(Serializable obj )
        throws IOException {
                                       
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(obj);
        oos.reset();
        byte[] b = baos.toByteArray();
        baos = null;
        /* ^^^^^^^^ WARNING-Java BUG! when serializing large objects
         * the baos seems to leak memory.  When this method returns
         * baos is unreferenced and SHOULD get all its space freed up,
         * but it does not appear to, unless we explicitly null it out.
         */
        return(b);
	
    } 


        /** {@inheritDoc}
         * 
         * @param B
         * @return object.
         * @throws ot.OtException
         */
    public Object byteArrayToObject(byte[] B )
        throws InvalidClassException, IOException, ClassNotFoundException {
        
        ByteArrayInputStream baos = new ByteArrayInputStream(B,0,B.length);
        Object obj = new Object();
            ObjectInputStreamWithPrimitiveSupport oos
                = new ObjectInputStreamWithPrimitiveSupport(baos);
        obj = oos.readObject();
        oos.close();
            baos = null;

        return(obj);
        
    }    
}
